package com.xunji.xunji.db.dao;

import android.content.Context;

import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.dao.Dao;
import com.xunji.xunji.db.DatabaseHelper;
import com.xunji.xunji.module.trace.dao.Trace;

import java.sql.SQLException;

public class TraceDao {
	private Dao<Trace, Integer> dao;

	public TraceDao(Context context) {
		DatabaseHelper dbHelper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
		try {
			dao = dbHelper.getDao(Trace.class);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	// 保存一条用户信息
	public void saveTrace(Trace bean) {
		try {
			// 如果表中没有该用户则保存，根据主键是否相同来标示是否是同一用户
			dao.createIfNotExists(bean);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	//根据id获取用户信息
	public Trace getTraceById(int id) {
		try {
			return dao.queryForId(id);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}

	}

	public void delete(int traceId){
		try {
			dao.deleteBuilder().where().eq("traceId",traceId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
